"""
    Controller for partner table.
"""

import turbogears
from mnemoply import model
from mnemoply.controllers.lookup import LookupController, TableCol, SQLObjectLookupDataSource
from mnemoply.controllers.crud import CRUDController

class PartnerController(LookupController, CRUDController):
    model_object = model.Partner
    lookup_columns = [
        TableCol(name='surname', caption=_('Surname')),
        TableCol(name='forename', caption=_('Forename')),
        TableCol(name='patronymic', caption=_('Patronymic')),
        TableCol(name='password', caption=_('Password')),
        TableCol(name='creationDate', caption=_('Creation date')),
        TableCol(name='phone', caption=_('Phone'), hidden=True),
        TableCol(name='email', caption=_('e-mail'), hidden=True),
        TableCol(name='amountOfLeaflets', caption=_('Amount of leaflets'), hidden=True),
        TableCol(name='comment', caption=_('Comment'), hidden=True),
    ]

    edit_fields = [
        'surname',
        'forename',
        'patronymic',
        'password',
        'phone',
        'email',
        'officeID',
        'partnerTypeID',
        'creationDate',
        'amountOfLeaflets',
        'comment',
        ]

    def __init__(self):
        LookupController.__init__(self,
            data_source=SQLObjectLookupDataSource(self.model_object),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
        )
        CRUDController.__init__(self, model_object=self.model_object)

    def defaultAddValues(self):
        current_user = turbogears.identity.current.user
        office_id = None
        if current_user.office is not None:
            office_id = current_user.office.id

        return dict(
            password = model.Partner.createNewPassword(current_user.office),
            officeID = office_id
            )

exposed = PartnerController()
